package com.agrimall.service.impl;

import com.agrimall.entity.Review;
import com.agrimall.mapper.ReviewMapper;
import com.agrimall.service.ReviewService;
import com.agrimall.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 商品评价服务实现类
 */
public class ReviewServiceImpl implements ReviewService {
    
    @Override
    public boolean addReview(Review review) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ReviewMapper mapper = sqlSession.getMapper(ReviewMapper.class);
            int result = mapper.insert(review);
            sqlSession.commit();
            return result > 0;
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            return false;
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Review> getReviewListByProduct(Long productId, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ReviewMapper mapper = sqlSession.getMapper(ReviewMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectByProductId(productId, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Review> getReviewListByUser(Long userId, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ReviewMapper mapper = sqlSession.getMapper(ReviewMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectByUserId(userId, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public Review getReviewByOrderId(Long orderId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ReviewMapper mapper = sqlSession.getMapper(ReviewMapper.class);
            return mapper.selectByOrderId(orderId);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public Review getReviewById(Long reviewId) {
        // 如果Mapper中没有这个方法，需要添加
        // 暂时返回null，后续可以添加
        return null;
    }
    
    @Override
    public int countReviewsByUser(Long userId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ReviewMapper mapper = sqlSession.getMapper(ReviewMapper.class);
            return mapper.countByUserId(userId);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public boolean deleteReview(Long reviewId, Long userId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ReviewMapper mapper = sqlSession.getMapper(ReviewMapper.class);
            int result = mapper.deleteById(reviewId, userId);
            sqlSession.commit();
            return result > 0;
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            return false;
        } finally {
            sqlSession.close();
        }
    }
}

